<template>
	<div class="add-company-manager">
		<div class="main_content">
			<div class="main_content_title">
				<div class="left">
					<span>{{ propUpType === 'add' ? $t('publicBtn.add') : propUpType === 'edit' ? $t('publicBtn.edit') :
						$t('publicBtn.detail') }}</span>
					<template v-for="(item, index) in $route.meta.title">
						<span v-if="languageId === item.langId">{{ item.name }}</span>
					</template>
				</div>
				<div class="right" />
			</div>
			<div class="main_content_con">
				<!-- 语言动态标签页面 -->
				<el-row :gutter="24">
					<el-col :span="20">
						<elTabPane :isShow="isTag" :tagId="tagId" @tagClick="tagClick"></elTabPane>
					</el-col>
				</el-row>
				<el-form ref="info" :model="info" :rules="rules" class="demo-ruleForm">
					<el-form v-for="(info, index) in info.list" :ref="`info${index}`" :model="info" :rules="rules"
						v-show="tagId === info.langId" :key="index" label-width="100px" class="demo-ruleForm">
						<div class="content_info">
							<template v-if="propUpType !== 'detail'">
								<el-row :gutter="24">
									<el-col :span="8">
										<el-form-item :label="$t('formLabel.navigation')" prop="navigationId">
											<el-select v-model="info.navigationId" filterable clearable
												:placeholder="$t('placeholderTip.pSelect')" style="width:100%"
												@change="copyValue('list', 'navigationId', info.navigationId)">
												<el-option v-for="item in navigatList" :key="item.id" :label="item.name" :value="item.id" />
											</el-select>
										</el-form-item>
									</el-col>
									<el-col :span="8">
										<el-form-item :label="$t('publicFormLabel.name')" prop="name">
											<el-input v-model="info.name" :placeholder="$t('placeholderTip.enter')" />
										</el-form-item>
									</el-col>
									<el-col :span="8">
										<el-form-item :label="$t('formLabel.partPlate')" prop="moduleId">
											<el-select v-model="info.moduleId" :placeholder="$t('placeholderTip.pSelect')" style="width:100%"
												@change="copyValue('list', 'moduleId', info.moduleId)">
												<el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id" />
											</el-select>
										</el-form-item>
									</el-col>
								</el-row>
								<el-row :gutter="24">
									<el-col :span="8">
										<el-form-item :label="$t('formLabel.linkName')" prop="urlName">
											<el-input :placeholder="$t('placeholderTip.pSelect')" v-model="info.urlName"
												class="input-with-select" @focus="getLinkDialog"
												@change="copyValue('list', 'urlName', info.urlName)">
												<el-button slot="append" icon="el-icon-search" @click.stop="getLinkDialog"></el-button>
											</el-input>
										</el-form-item>
									</el-col>
									<el-col :span="8">
										<el-form-item :label="$t('publicFormLabel.sort')" prop="sort">
											<el-input v-model="info.sort" @mousewheel.native.prevent @keydown.native.up.prevent
												@keydown.native.down.prevent @keydown.native.tab.prevent
												@change="copyValue('list', 'sort', info.sort)" :placeholder="$t('placeholderTip.enter')" />
										</el-form-item>
									</el-col>
									<el-col :span="8">
										<el-form-item :label="$t('formLabel.parameterCode')" prop="status">
											<el-select v-model="info.code" filterable clearable :placeholder="$t('placeholderTip.pSelect')"
												style="width:100%" @change="copyValue('list', 'code', info.code)">
												<el-option v-for="item in codeList" :key="item.code" :label="item.name" :value="item.code" />
											</el-select>
										</el-form-item>
									</el-col>
								</el-row>
								<el-row :gutter="24">
									<el-col :span="8">
										<el-form-item :label="$t('formLabel.featureIcon')" prop="icon">
											<div class="avatar-uploader">
												<upload-img customsrc="true" :defsrc="info.icon"
													:on-success="function (res, file) { return handleAvatarSuccess(res, file, info, 'icon') }">
													<i class="el-icon-plus avatar-uploader-icon" />
												</upload-img>
											</div>
											<div class="tipInfo">（{{ $t('text.size') }}50px*50px）</div>
											<div v-if="info.icon" class="deleteImg" @click.stop="deleteImg(info, 'icon')">
												<img src="@/assets/images/close.png">
											</div>
										</el-form-item>
									</el-col>
								</el-row>
								<el-row :gutter="24">
									<el-col :span="8">
										<el-form-item :label="$t('publicFormLabel.remark')" prop="remark">
											<el-input v-model="info.remark" rows="3" type="textarea"
												@change="copyValue('list', 'remark', info.remark)" :placeholder="$t('placeholderTip.enter')" />
										</el-form-item>
									</el-col>
								</el-row>
							</template>
							<template v-else>
								<el-row :gutter="24">
									<el-col :span="8">
										<el-form-item :label="$t('formLabel.navigation')" prop="navigationId">
											<el-input v-model="info.navigationName" />
										</el-form-item>
									</el-col>
									<el-col :span="8">
										<el-form-item :label="$t('publicFormLabel.name')" prop="name">
											<el-input v-model="info.name" />
										</el-form-item>
									</el-col>
									<el-col :span="8">
										<el-form-item :label="$t('formLabel.partPlate')" prop="moduleId">
											<el-input v-model="info.moduleName" />
										</el-form-item>
									</el-col>
								</el-row>
								<el-row :gutter="24">
									<el-col :span="8">
										<el-form-item :label="$t('formLabel.linkName')" prop="urlName">
											<el-input v-model="info.urlName" />
										</el-form-item>
									</el-col>
									<el-col :span="8">
										<el-form-item :label="$t('publicFormLabel.sort')" prop="sort">
											<el-input v-model="info.sort" />
										</el-form-item>
									</el-col>
									<el-col :span="8">
										<el-form-item :label="$t('formLabel.parameterCode')" prop="status">
											<el-select v-model="info.code" disabled style="width:100%">
												<el-option v-for="item in codeList" :key="item.code" :label="item.name" :value="item.code" />
											</el-select>
										</el-form-item>
									</el-col>
								</el-row>
								<el-row :gutter="24">
									<el-col :span="8">
										<el-form-item :label="$t('formLabel.featureIcon')" prop="icon">
											<div class="avatar-uploader">
												<div class="avatar-uploader">
													<upload-img :preview="true" :defsrc="info.icon" disabled />
												</div>
											</div>
										</el-form-item>
									</el-col>
								</el-row>
								<el-row :gutter="24">
									<el-col :span="8">
										<el-form-item :label="$t('publicFormLabel.remark')" prop="remark">
											<el-input v-model="info.remark" />
										</el-form-item>
									</el-col>
								</el-row>
							</template>
						</div>
					</el-form>
				</el-form>
				<div class="footerBtn">
					<el-button @click="getCancle">{{ propUpType !== 'detail' ? $t('el.popconfirm.cancelButtonText') :
						$t('el.popconfirm.close') }}</el-button>
					<el-button v-if="propUpType !== 'detail'" type="primary" @click="addSubmitForm"
						v-loading.fullscreen.lock="fullscreenLoading">{{ $t('el.popconfirm.confirmButtonText') }}</el-button>
				</div>
			</div>
		</div>

		<!-- 链接弹框 -->
		<chooseLink ref="chooseLink" @change="getLinkData"></chooseLink>
	</div>
</template>
<script>
import {
	getWorkInfo,
	setWorkSave,
	setWorkUpdate,
	getWorkCodeList,
	getModuleModuleList,
	getNavigatios
} from '@/api/app-set'
import chooseLink from '@/components/choose-link'
import UploadImgSimple from '@/components/Upload/UploadImgSimple'
import elTabPane from '@/components/el-tab-pane/index'; //语言动态标签页面
import defaultSettings from '@/settings'
export default {
	name: 'BlosManagerInfo',
	components: {
		elTabPane,
		chooseLink,
		UploadImg: UploadImgSimple,
	},
	data() {
		return {
			tagId: '',//语言id 默认id 
			isTag: false,
			BASE_IMG: defaultSettings.BASE_IMG,
			limit: 5,
			info: {
				list: []
			},
			oldNavigationId: '',//保留上一个导航Id
			navigatList: [],//导航
			codeList: [],//参数编码
			typeList: [],//所属板块
			rules: {

				navigationId: [
					{
						required: true,
						message: this.$t('placeholderTip.pSelect'),
						trigger: 'change'
					}
				],

				name: [
					{
						required: true,
						message: this.$t('placeholderTip.enter'),
						trigger: 'blur'
					}
				],
				icon: [
					{
						required: true,
						message: this.$t('placeholderTip.pUpload'),
						trigger: 'change'
					}
				],
				countryId: [
					{
						required: true,
						message: this.$t('placeholderTip.pSelect'),
						trigger: 'change'
					}
				],
				navigationId: [
					{
						required: true,
						message: this.$t('placeholderTip.pSelect'),
						trigger: 'change'
					}
				],
				moduleId: [
					{
						required: true,
						message: this.$t('placeholderTip.pSelect'),
						trigger: 'change'
					}
				],
				urlName: [
					{
						required: true,
						message: this.$t('placeholderTip.pSelect'),
						trigger: 'change'
					}
				],
				name: [
					{
						required: true,
						message: this.$t('placeholderTip.enter'),
						trigger: 'blur'
					}
				],
				sort: [
					{
						required: true,
						message: this.$t('placeholderTip.enter'),
						trigger: 'blur'
					},
				]
			},
			countryList: [], // 省列表
			oblastList: [], // 市列表
			cityList: [], // 区列表
			propUpType: '', // 'add' edit detail
			pickerOptions: {
				disabledDate(time) {
					return time.getTime() < Date.now() - 8.64e7
				}
			},
			fullscreenLoading: false,
		}
	},
	computed: {
		languageId() {
			// 固定语种ID  1:中文  2:英文 3:俄文 4:乌语
			return this.$store.getters.languageId
		},
		languageList() {
			return this.$store.getters.languageList.length > 0 ? this.$store.getters.languageList : JSON.parse(localStorage.getItem('languageList'))
		}
	},
	watch: {

	},
	mounted() {
		window._vm = this
		this.propUpType = this.$route.query.propUpType
		this.isTag = true;
		this.tagId = this.languageId;
		if (this.$route.query.propUpType === 'add') {
			this.addData()
			this.getWorkCodeListData()
			this.getNavigatiosData()
		} else if (this.$route.query.id) {
			if (this.$route.query.propUpType === 'edit') {
				this.getNavigatiosData()
			}
			this.getWorkCodeListData()
			this.getInfo(this.$route.query.id)
		}
	},
	created() {
		console.log('created')
	},
	activated() {

	},

	destroyed() {
	},
	methods: {
		// 导航列表
		getNavigatiosData() {
			getNavigatios().then(res => {
				if (res.code === 200) {
					this.navigatList = res.data
				}
			})
		},
		// 语言菜单处理
		tagClick(id) {
			if (id !== this.tagId) {
				this.resetForm('info', 2)
				this.tagId = id
			}
		},
		addData() {
			var arr = []
			this.languageList.map(i => {
				var obj = {
					langId: i.id,
					navigationId: '', //导航id
					navigationName: '', // 导航
					name: '', // 功能名称
					moduleId: '', //功能板块Id
					moduleName: '', //功能板块
					urlId: '', //链接id
					urlName: '', //链接名称
					icon: '',
					sort: '',
					remark: '', // 备注，
					status: 1, //状态 -1:删除 0:无效 1:有效
					code: '' //参数编码
				}
				arr.push(obj)
			})
			this.info.list = arr
		},
		// 重置表单
		resetForm(vaid, type) {
			// type 1 重置表单 2仅清空校验
			if (this.$refs[vaid] !== undefined) {
				this.$nextTick(() => {
					Promise.all([
						//数组部分的表单，用map返回验证函数的调用
						...this.info.list.map((item, index) => {
							var refs = `info${index}`
							let valiForm = this.$refs?.[refs].validate
								? this.$refs?.[refs]
								: this.$refs?.[refs][0];
							if (type === 1) {
								valiForm.resetFields()
							} else {
								valiForm.clearValidate()
							}
						})

					])
				})

			}
		},
		// 链接弹框
		getLinkDialog() {
			this.$refs.chooseLink.getOpen(this.info.list[0].urlId)
		},
		getLinkData(urlId, urlName) {
			this.info.list.map(i => {
				i.urlId = urlId
				i.urlName = urlName
			})
		},
		// 编码list
		getWorkCodeListData() {
			getWorkCodeList().then(res => {
				if (res.code === 200) {
					this.codeList = res.data
				}
			})
		},
		// 所属板块 列表
		getNavigationData(id) {
			getModuleModuleList(id).then(res => {
				if (res.code === 200) {
					this.typeList = res.data
				}
			})
		},

		// 无多语言字段处理
		copyValue(key, key2, value) {
			this.info[key].map(i => {
				if (key2 === 'navigationId') {
					this.typeList = []
					if (this.oldNavigationId && this.oldNavigationId !== value) {
						i.moduleName = ''
						i.moduleId = ''
					}
				}
				i[key2] = value
			})
			if (key2 === 'navigationId') {
				this.getNavigationData(value)
				this.oldNavigationId = value
			}

		},
		deleteImg(tar, key) {
			this.info.list.map(i => {
				i[key] = ''
			})
		},
		handleAvatarSuccess(res, file, tar, key) {
			if (res) {
				this.info.list.map(i => {
					i[key] = res.key
				})
			}
		},
		// 提交
		addSubmitForm() {
			var list = JSON.parse(JSON.stringify(this.info.list))
			var flag = false
			for (var i = 0; i < list.length; i++) {
				var item = list[i]
				var index = i
				// 当前语种 表单校验every
				if (this.tagId === item.langId) {
					let valiForm = this.$refs?.[`info${index}`].validate
						? this.$refs?.[`info${index}`]
						: this.$refs?.[`info${index}`][0];
					valiForm.validate((res) => {
						if (res) {
							flag = true // 一个多语言字段 直接校验当前表单 
							// flag = this.getCheck(index) //多个多语言 需要校验其他语种表单
						} else {
							return false
						}
					})
				}
			}
			if (flag) {
				// 全部表单验证通过
				var arr = JSON.parse(JSON.stringify(this.info.list))
				this.fullscreenLoading = true
				if (this.propUpType === 'add') {
					setWorkSave({
						t: arr
					})
						.then(res => {
							if (res.code === 200) {
								this.$message({
									showClose: true,
									message: this.$t('text.addSucceedTip'),
									type: 'success'
								})
								this.fullscreenLoading = false
								this.getCancle()
							}
						})
						.catch(err => {
							this.fullscreenLoading = false
						})

				} else {
					setWorkUpdate({
						t: arr
					})
						.then(res => {
							if (res.code === 200) {
								this.$message({
									showClose: true,
									message: this.$t('text.editSucceedTip'),
									type: 'success'
								})
								this.fullscreenLoading = false
								this.getCancle()
							}
						})
						.finally(() => {
							this.fullscreenLoading = false
						})
				}
			}
		},
		// 详情
		getInfo(id) {
			const loading = this.$loading({
				lock: true,
				text: this.$t('text.loading'),
				spinner: 'el-icon-loading',
				background: 'rgba(0, 0, 0, 0.7)'
			})
			getWorkInfo(id).then(res => {
				if (res.code === 200) {
					var arr = res.data.t || []
					if (arr.length > 0) {
						this.setInfoData(arr)
					}
					loading.close()
				}
			}).catch(() => {
				loading.close()
			})
		},

		// 详情数据处理
		async setInfoData(data) {
			if (this.propUpType === 'edit') {
				if (data[0].navigationId) {
					this.getNavigationData(data[0].navigationId)
				}
			}
			this.$nextTick(() => {
				var newArr = []
				var arr = data
				var obj = arr[0]

				if (this.propUpType !== 'detail') {
					this.languageList.map(i => {
						var obj_1 = {
							langId: i.id,
							id: obj.id,
							navigationId: obj.navigationId, //导航id
							navigationName: obj.navigationName, // 导航
							name: '', // 功能名称-多语言字段
							moduleId: obj.moduleId, //功能板块Id
							moduleName: obj.moduleName, //功能板块
							urlId: obj.urlId, //链接id
							urlName: obj.urlName, //链接名称
							icon: obj.icon,
							sort: obj.sort,
							remark: obj.remark, // 备注，
							status: obj.status, //状态 -1:删除 0:无效 1:有效
							code: obj.code //参数编码
						}
						newArr.push(obj_1)
					})
				} else {
					this.languageList.map(i => {
						var obj = {
							langId: i.id,
							id: '',
							navigationId: '', //导航id
							navigationName: '', // 导航
							name: '', // 功能名称
							moduleId: '', //功能板块Id
							moduleName: '', //功能板块
							urlId: '', //链接id
							urlName: '', //链接名称
							icon: '',
							sort: '',
							remark: '', // 备注，
							status: 1, //状态 -1:删除 0:无效 1:有效
							code: '' //参数编码
						}
						newArr.push(obj)
					})
				}
				arr.map(j => {
					// 根据id进行对象替换
					let index = newArr.findIndex(e =>
						e.langId == j.langId
					)
					if (index > -1) {
						// 替换对象
						newArr[index] = j
					}
				})
				this.info.list = newArr
			})
		},
		// 取消
		getCancle() {
			this.$store.state.tagsView.visitedViews.splice(
				this.$store.state.tagsView.visitedViews.findIndex(
					item => item.path === this.$route.path
				),
				1
			)
			this.$router.go(-1)
		}
	}
}
</script>
<style lang="scss" scoped>
.add-company-manager {
	height: 100%;
	width: 100%;
	padding: 20px;
	box-sizing: border-box;
	overflow: hidden;

	&>.main_content {
		width: 99.9%;
		height: 100%;

		&>.main_content_title {
			margin-bottom: 10px;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
		}

		.main_content_con {
			height: calc(100% - 40px);
			width: 100%;
			display: inline-block;
			border: 1px solid #dfe6ec;
			padding: 20px;


			&>.el-form {
				width: 100%;
				height: calc(100% - 108px);
				margin-top: 20px;
				overflow: hidden;

				&>.el-form {
					width: 100%;
					height: 100%;
				}

				::v-deep .content_info {
					width: 100%;
					height: 100%;
					overflow: hidden;
					overflow-y: auto;

					.line {
						height: 1px;
						border: 1px solid #ebeef5;
					}

					.el-table {
						::v-deep th {
							line-height: 40px;
							padding: unset;
							background-color: #f2f2f2;
						}
					}
				}


			}

			.footerBtn {
				width: 100%;
				display: flex;
				justify-content: center;
				padding-top: 10px;
			}

			.cardBox {
				width: 104px;
				height: auto;
				float: left;
				position: relative;

				&>.name {
					text-align: center;
					color: #999;
					font-size: 12px;
				}
			}

			.el-button:hover,
			.el-button:focus {
				color: #000;
			}
		}
	}
}
</style>

